import uuid
from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework import exceptions

from login import models


# 进行登录的序列化操作-进行用户的密码登录验证
class registerSerializers(serializers.ModelSerializer):
    confirm_password = serializers.CharField(write_only=True)
    class Meta:
        model = models.userInfo
        fields = ['id', 'username', 'password', 'confirm_password', "sex", 'email']
        extra_kwargs = {
            "id": {"read_only": True},
            "password": {"write_only": True}
        }
    def validate_password(self, value):
        return value

    def validate_confirm_password(self, value):
        password = self.initial_data.get("password")
        if password != value:
            raise exceptions.ValidationError("密码不一样")
        return value
class RegistesView(APIView):
    authentication_classes = []

    def post(self, request, *args, **kwargs):
        ser = registerSerializers(data=request.data)
        if ser.is_valid():
            ser.validated_data.pop("confirm_password")
            ser.save()
            return Response({"code": 200, "message": "成功", "data": ser.data})
        else:
            return Response({"code": 1000, "errors": "注册失败", "data": ser.errors})

class UserLogin(APIView):
    def login(request):

        return render(request, {"form": 111})

class Test(APIView):
    def returnData(self):
        data=models.userInfo.objects.filter(id=1)

        return HttpResponse(data)